package com.neomanager.admin.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.neomanager.admin.dao.ContentDao;
import com.neomanager.login.model.AdminInfo;
import com.util.UtilCommon;

@Controller
@SessionAttributes("adminInfo")
public class ContentController {

	private final Logger logger = Logger.getLogger(this.getClass());
	@Resource(name="ContentDao")
	ContentDao content;

	/**
	 * 콘텐츠 목록
	 */
	@RequestMapping(value = "/admin/contentmng_list.do", method = RequestMethod.GET)
	public String listContentmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("콘텐츠 목록.");
	    HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		// 현재 페이지
		int page = 1;
		if (commandMap.get("page") != null)
			page = Integer.parseInt(commandMap.get("page").toString());

		// 페이지당 목록 수
		int pageCount = 10;

		// 시작번호
		int startNo = (page - 1) * pageCount;

		commandMap.put("page", page);
		commandMap.put("pageCount", pageCount);
		ArrayList<Map<String, Object>> listMap = content.selectContentInfo(commandMap);

		// 총 갯수
		int totalCount = 0;
		if ( listMap != null && listMap.size() > 0 )
		{
			Map<String, Object> rs = listMap.get(0);
			totalCount = Integer.parseInt( rs.get("TOTALCOUNT").toString() );
		}

		// 페이징 처리
		if (totalCount > 0)
		{
			UtilCommon util = new UtilCommon();
			// 총게시물수, 페이지당 목록수, 현재페이지, 블럭당 나타낼 페이지 수
			util.setPage(totalCount, pageCount, page, 10);
			commandMap.put("totalCount", totalCount);
			commandMap.put("totalPage", util.getTotalPage());
			commandMap.put("startPage", util.getStartPage());
			commandMap.put("endPage", util.getEndPage());
			commandMap.put("startNo", startNo);
		}

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);
		model.addAttribute("commandMap", commandMap);
		model.addAttribute("listMap", listMap);

		return "admin/contentmng_list";
	}

	/**
	 * 콘텐츠 등록 화면
	 */
	@RequestMapping(value = "/admin/contentmng_write.do", method = RequestMethod.GET)
	public String writeContentmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("콘텐츠 등록 화면.");
	    HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);

		return "admin/contentmng_write";
	}

	/**
	 * 콘텐츠 수정 화면
	 */
	@RequestMapping(value = "/admin/contentmng_edit.do", method = RequestMethod.GET)
	public String editContentmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("콘텐츠 수정 화면.");
	    HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		Map<String, Object> listMap = content.selectContentInfoById(commandMap);

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);
		model.addAttribute("listMap", listMap);

		return "admin/contentmng_edit";
	}

	/**
	 * 콘텐츠 등록/수정/삭제 처리
	 */
	@RequestMapping(value = "/admin/contentmng_proc.do", method = RequestMethod.POST)
	public String processAdminmng(@RequestParam Map<String, Object> commandMap, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.debug("콘텐츠 등록/수정/삭제 처리.");

		String redirectUrl = "redirect:../admin/contentmng_list.do";
		if(commandMap.get("mode").toString().equals("write"))
		{
			String sys_id = UtilCommon.idMake("");
			commandMap.put("content_sid", sys_id);

			content.insertContentInfo(commandMap);
		}
		else if(commandMap.get("mode").toString().equals("edit"))
		{
			content.updateContentInfo(commandMap);
		}
		// 콘텐츠 수정 (개별관리 권한)
		else if(commandMap.get("mode").toString().equals("cedit"))
		{
			content.updateContentInfo(commandMap);
			String content_sid = commandMap.get("content_sid").toString();
			redirectUrl = "redirect:../content/" + content_sid + "_view.do";
		}
		else if(commandMap.get("mode").toString().equals("delete"))
		{
			content.deleteContentInfo(commandMap);
		}

		return redirectUrl;
	}

	/**
	 * 콘텐츠 보기 화면
	 */
	@RequestMapping(value = "/content/{sid}_view.do", method = RequestMethod.GET)
	public String viewContentmng(@PathVariable String sid, Model model,
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("콘텐츠 보기 화면.");
		System.out.println( sid );
	    HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		Map<String, Object> commandMap = new HashMap<String, Object>();
		commandMap.put("content_sid", sid);

		Map<String, Object> listMap = content.selectContentInfoById(commandMap);

		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);
		model.addAttribute("listMap", listMap);

		return "admin/contentmng_view";
	}

}
